package chap2_observer;

import java.util.ArrayList;

public class WeatherData implements Subject {
    
    /**
     * Observer 객체들을 저장하기 위한 저장소
     */
    private ArrayList<Observer> observers;
    private float tempeature;
    private float humidity;
    private float pressure;

    public WeatherData() {
        observers = new ArrayList<Observer>(); // 저장소 생성
    }
    
    @Override
    public void registerObserver(Observer o) {
        observers.add(o);
    }

    @Override
    public void removeObserver(Observer o) {
        observers.remove(o);
    }

    @Override
    public void notifyObservers() {
        for (int i = 0; i < observers.size(); i++) {
            Observer observer = (Observer)observers.get(i);
            observer.update(tempeature, humidity, pressure);
        }
    }
    
    /**
     * 기상스테이션으로 측정치가 갱신되었음
     */
    public void measurementsChanged() {
        notifyObservers();
    }
    
    public void setMeasurements(float temperature, float humidity, float pressure) {
        this.tempeature = temperature;
        this.humidity = humidity;
        this.pressure = pressure;
        measurementsChanged();
    }

}
